package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

import static com.itheima.constant.MessageConstant.*;

@Service(version = "1.0",interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Resource
    CheckGroupMapper checkGroupMapper;
    @Resource
    CheckItemMapper checkItemMapper;

    @Override
    public Result save(CheckGroup checkGroup) {
        try {
            checkGroupMapper.save(checkGroup);
            saveGroupAndItem(checkGroup, checkGroup.getId());
            return Result.success(ADD_CHECKGROUP_SUCCESS, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail(ADD_CHECKGROUP_FAIL);
    }

    @Override
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

            Page<CheckGroup> checkGroupPage = checkGroupMapper.findByCondition(queryPageBean.getQueryString());
            PageResult pageResult = new PageResult();
            pageResult.setRows(checkGroupPage.getResult());
            pageResult.setTotal(checkGroupPage.getTotal());

            return Result.success(QUERY_CHECKGROUP_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail(QUERY_CHECKGROUP_FAIL);
    }

    @Override
    public Result findById(Integer id) {
        /*this.tableData = res.data.data.checkItemList;
                    this.checkItemIds = res.data.data.checkItemIds;
                    this.formData = res.data.data.checkGroup;*/
        try {
            List<CheckItem> checkItemList = checkItemMapper.findAll();
            List<Integer> checkItemIds = checkGroupMapper.findItemIdByGroupId(id);
            CheckGroup checkGroup = checkGroupMapper.findById(id);

            HashMap<String, Object> map = new HashMap<>();

            map.put("checkItemList",checkItemList);
            map.put("checkItemIds",checkItemIds);
            map.put("checkGroup",checkGroup);

            return Result.success(QUERY_CHECKGROUP_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail(QUERY_CHECKGROUP_FAIL);
    }

    @Override
    public Result update(CheckGroup checkGroup) {
        try {
            checkGroupMapper.update(checkGroup);

            Integer groupId = checkGroup.getId();
            checkGroupMapper.deleteGroupAndItemByGroupId(groupId);

            saveGroupAndItem(checkGroup, groupId);

            return Result.success(EDIT_CHECKGROUP_SUCCESS,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail(EDIT_CHECKGROUP_FAIL);
    }

    private void saveGroupAndItem(CheckGroup checkGroup, Integer groupId) {
        List<Integer> checkitemIds = checkGroup.getCheckItemIds();
        for (Integer checkitemId : checkitemIds) {
            checkGroupMapper.saveGroupAndItem(groupId, checkitemId);
        }
    }

    @Override
    public Result delete(Integer id) {
        long count = checkGroupMapper.count(id);
        if (count>0){
            return Result.fail(500,DELETE_CHECKGROUP_FAIL);
        }
        checkGroupMapper.delete(id);
        return Result.success(DELETE_CHECKGROUP_SUCCESS,null);
    }

    @Override
    public Result findAllCheckGroup() {
        try {
            List<CheckGroup> checkGroupList = checkGroupMapper.findAll();
            return Result.success(QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail(QUERY_CHECKGROUP_FAIL);
    }
}
